//index.js
//获取应用实例
let api = require("../../utils/api.js");//引入API
let util = require("../../utils/util.js");//引入API
const app = getApp()

Page({
  data: {
      weather:[],
      hourly :[]
  },
  onLoad: function () {
      var chat = this;
      api.getLocation()
        .then(api.getCityId)
        .then(json => {
            var id = json.townID;
            api.getNowWeather(id)
            .then(res =>{
                res.format_last_update = util.formatTime(res.last_update);
                res.bg = util.getBackground(res.now.code);
                chat.setData({
                    weather: res
                });
                app.globalData.weather = res;//保存天气数据
                api.get24hWeather(id)
                    .then(hour => {
                        hour.forEach(val =>{
                            val.img = `../../images/weather/${val.code}.png`;
                            val.format_time = util.formatHour(val.time);
                        })
                        chat.setData({
                            hourly: hour
                        })
                    })
            });
        });
  }
})
